#include "kolobok.h"
#include <stdlib.h>
#include "animation.h"
#include "statectrl.h"
#include "point2.h"
#include "circle.h"
#include "game.h" //?

Kolobok::Kolobok(){

//	ani_still =NULL;	
//	ani_fall =NULL;
//	ani_jump =NULL;
	circle =NULL;
}

Kolobok::~Kolobok(){

	delete circle;
	circle =NULL;

//	delete ani_still;	
//	ani_still =NULL;	
//	delete ani_fall;
//	ani_fall =NULL;
//	delete ani_jump;
//	ani_jump =NULL;
}

/*
void Kolobok::setState(int state, int argument){
	
	switch(state){
	case STATE_DEAD:
//		cur_animation =ani_dead;
//		set_func =characterDead;
		break;
	case STATE_STILL:
//		cur_animation =ani_still;
//		set_func =kolobokStill;
		break;
//	case STATE_MOVE:
//		cur_animation =ani_run;
//		set_func =characterMove;
//		break;
	case STATE_FALL:
//		cur_animation =ani_fall;
//		set_func =kolobokFall;
		break;
	case KOLOBOK_STATE_JUMP:
//		cur_animation =ani_fall;
//		set_func =kolobokJump;
		break;
	}

//	cur_animation->begin_time =0;
//	cur_animation->cur_frame =0;
	this->state =state;
}
*/

void Kolobok::update(Level* level, Point2* acc, float dt){

	switch(state){
	case STATE_STILL:
		still(level, acc, dt);
		break;
	case STATE_FALL:
		fall(level, acc, dt);
		break;
	case KOLOBOK_STATE_JUMP:
		jump(level, acc, dt);
		break;
	case STATE_DEAD:
		die(level, acc, dt);
		break;
	}
//	set_func(level, kolobok, acc, dt);
}

void Kolobok::still(Level* level, Point2* acc, float dt){

	if(moveXplus){
		velocity->x =speed;
//		kolobok->orientation->x =1;
//		kolobok->setState(STATE_MOVE, 0);
//		return;
	}
	else if(moveXminus){
		velocity->x =-speed;	
//		kolobok->orientation->x =-1;
//		kolobok->setState(STATE_MOVE, 0);
//		return;
	}
	else{
	
		velocity->x =0;
	}

	if(special){
		velocity->y =jump_speed;
//		setState(KOLOBOK_STATE_JUMP, 0);
		setJump(0);
		return;
	}
}

void Kolobok::jump(Level* level, Point2* acc, float dt){

	Point2 v;

	// find velocity vector
	*(velocity) += *acc*(dt*0.001f*antigravitator);
	if(moveXplus){
		velocity->x =speed;
//		kolobok->orientation->x =1;
	}
	else if(moveXminus){
		velocity->x =-speed;	
//		character->orientation->x =-1;
	}
	else{
	
		velocity->x =0;
	}

	// find translation vector
	v =*velocity*(float)dt*0.1f; // 100cm/1000ms

	//  if collided
//	if(collideToLevel(level, rect, position, &v, &t, &n, LEVEL|PLATFORMS|CIRCLES)){
//		// move hunter to the point of collision
//		*hunter->position +=v*t;
//
//		v -=v*t;
//		// if there was a collision - slide by the new normal
//		getSlideVector(hunter, &v, &n, &sld);
//
//		if(collideToLevel(level, rect, position, &sld, &t, &n, LEVEL|PLATFORMS|CIRCLES))
//			*(hunter->position) +=sld*t;
//		else
//			*(hunter->position) +=sld;
//	}
//	else{

//		*(((Kolobok*)kolobok)->circle->origin) +=v;
		*position +=v;
//	}
	// if the peak was reached - begin to fall
	if(velocity->y <0){
//		setState(STATE_FALL, 0);
		setFall(0);
	}
}

void Kolobok::fall(Level* level, Point2* acc, float dt){

	Point2 v, n, sld;
	float t;
	Object* object =NULL;
	Game* game =Game::Instance();

	// find velocity vector
	*velocity += *acc*(dt*0.001f*antigravitator);
	if(moveXplus){
		velocity->x =speed;
//		kolobok->orientation->x =1;
	}
	else if(moveXminus){
		velocity->x =-speed;	
//		character->orientation->x =-1;
	}
	else{
	
		velocity->x =0;
	}

	// find translation vector
	v =*velocity*(float)dt*0.1f; // 100cm/1000ms

	// if the character stands on any object - stop falling
	// else - continue to fall
	if(game->standsCircleAtLevel(level, circle->radius_ph, position, &n, &object)){
//		setState(STATE_STILL, 0);
		setStill(0);
	}
	else{

		// if there was a collision, and the character is not standing
		// move to the point of collision and fall down
		// esle - continue to fall
		if(game->collideCircleToLevel(level, circle->radius_ph, position, &v, &t, &n, LEVEL|PLATFORMS|FLOORS|SLOPES|CIRCLES, &object)){
			
//			*(((Kolobok*)kolobok)->circle->origin) +=v*t;
			*position +=v*t;

			v -=v*t;
			v.x =0;
			if(game->collideCircleToLevel(level, circle->radius_ph, position, &v, &t, &n, LEVEL|PLATFORMS|FLOORS|SLOPES|CIRCLES, &object))
//				*(((Kolobok*)kolobok)->circle->origin) +=v*t;
				*position +=v*t;
			else
//				*(((Kolobok*)kolobok)->circle->origin) +=v;
				*position +=v;
		}
		else{
//			*(((Kolobok*)kolobok)->circle->origin) +=v;
			*position +=v;
		}
	}
}

void Kolobok::die(Level* level, Point2* acc, float dt){

	// find velocity vector
	*velocity += *acc*(dt*0.001f);
	// find translation vector
	*position +=*velocity*(float)dt*0.1f; // 100cm/1000ms
}

float Kolobok::get_xLo(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->xLo;
}

float Kolobok::get_xHi(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->xHi;
}

float Kolobok::get_yHi(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->body->yHi;
}

float Kolobok::get_yLo(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->body->yLo;
}

float Kolobok::get_pic_xLo(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->xLo;
}

float Kolobok::get_pic_xHi(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->xHi;
}

float Kolobok::get_pic_yHi(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->body->yHi;
}

float Kolobok::get_pic_yLo(){

	return 1;//position->x + cur_animation->frames[cur_animation->cur_frame]->body->yLo;
}

Rect* Kolobok::getBody(){

	return NULL;
}

Rect* Kolobok::getBox(){

	return NULL;
}

